/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uniriotec.pm.prova.dao.postgresql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import uniriotec.pm.prova.dao.DisciplinaDAO;
import uniriotec.pm.prova.dao.TopicoDAO;
import uniriotec.pm.prova.dto.DisciplinaDTO;
import uniriotec.pm.prova.dto.TopicoDTO;
import uniriotec.pm.prova.model.Disciplina;
import uniriotec.pm.prova.model.Topico;

/**
 *
 * @author albertoapr
 */
public class PostgresqlTopicoDAO extends PostgresqlDB implements TopicoDAO {
	/** The Constant LOG. */
	 private static final Logger LOG = Logger.getLogger(PostgresqlDisciplinaDAO.class);
    @Override
    public void create(TopicoDTO topico) 
            {
       try {
            String sql = "insert into topico select nextval('topico_id_seq') as id,? as disciplinaid, ? as nome";
            this.criaConexao(false);
            PreparedStatement stmt = null;
            stmt = this.getConnection().prepareStatement(sql);
            stmt.setInt(1, topico.getDisciplinaId());
	    stmt.setString(2, topico.getNome());
            stmt.execute();
            
            this.getConnection().commit();
            this.getConnection().close();
                    
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    	
	}
    @Override
    public void remove(int topicoId)  {
        
        
        try {
        
            this.criaConexao(true);
                   
                   PreparedStatement stmt = null;
                   stmt = this.getConnection().prepareStatement("delete from topico where id ="+  Integer.toString(topicoId));
                    stmt.execute();
                   
                   this.getConnection().close();
                    
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    	
	}
    @Override
    public void update(TopicoDTO topico) {
        
       try {
            String sql = "update topico set nome = ?, disciplinaid = ? where id = ?";
            this.criaConexao(false);
            PreparedStatement stmt = null;
            stmt = this.getConnection().prepareStatement(sql);
            stmt.setInt(2, topico.getDisciplinaId());
	    stmt.setString(1, topico.getNome());
            stmt.execute();
            this.getConnection().close();
                    
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    	
	}
    @Override
    public ArrayList<TopicoDTO> listAll() {
        ArrayList<TopicoDTO> lista = null;
        
        try {
        
            this.criaConexao(false);
                   
                   PreparedStatement stmt = null;
                   stmt = this.getConnection().prepareStatement("select id,disciplinaid,nome from topico");
                   ResultSet rs = stmt.executeQuery();
                   lista = new ArrayList<TopicoDTO>();
                   while(rs.next()){
                       Topico t = new Topico();
                       t.setId(Integer.parseInt(rs.getString("id")));
                       t.setDisciplinaId(Integer.parseInt(rs.getString("disciplinaid")));
                       t.setNome(rs.getString("nome"));
                       lista.add(new TopicoDTO(t));
                   }
                   this.getConnection().close();
                    
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    	return lista;	
    }
    @Override
    public TopicoDTO searchById(int topicoId) {
       
        try {
            TopicoDTO dto = null;
            this.criaConexao(false);
            String sql = "select id,disciplinaid, nome from topico where id = ?";
            PreparedStatement stmt = null;
            ResultSet rs = null;
            
            try {
                    stmt = this.getConnection().prepareStatement(sql);
                    stmt.setInt(1, topicoId);
                    rs = stmt.executeQuery();
                    if (rs.next()) {
                            Topico topico = new Topico();
                            topico.setId(rs.getInt("id"));
                            topico.setDisciplinaId(Integer.parseInt(rs.getString("disciplinaId")));
                            topico.setNome(rs.getString("nome"));
                            dto = new TopicoDTO(topico);
                    }
            } catch (SQLException e) {
                    throw e;
            } finally {
                    try {
                            stmt.close();
                            rs.close();
                            this.fechaConexao();
                            return dto;
                    } catch (SQLException e) {
                            throw e;
                    }

            }
           
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(PostgresqlDisciplinaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null; 
    }
    

    
    @Override
    public void addQuestao(int topicoId, int questaoId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void removeQuestao(int topicoId, int questaoId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Set<Questao> listQuestoes(int topicoId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    

    

   
    
}
